//
//  YSTabButton.m
//  YiShi
//
//  Created by myMac on 15/7/3.
//  Copyright (c) 2015年 myMac. All rights reserved.
//

#import "YSTabButton.h"

// Padding of the content
static const float kPadding = 4.0;

// Margin between the image and the title
static const float kMargin = 2.0;

// Margin at the top
static const float kTopMargin = 2.0;
@interface YSTabButton ()
@property(nonatomic,strong) UIImageView *tabImageView;
@property(nonatomic,strong) UILabel *tabTitleLabel;
@end
@implementation YSTabButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)init {
    if (self = [super init]) {
        self.bounds = CGRectMake(0, 0, 80, 40);
        [self setupSubViews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.bounds = CGRectMake(0, 0, 80, 40);
        [self setupSubViews];
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    [self setupSubViews];
}

- (void)setupSubViews {
    
    CGRect rect = self.bounds;
    
    CGRect container = CGRectInset(rect, kPadding, kPadding);
    container.size.height -= kTopMargin;
    container.origin.y += kTopMargin;
    
    //设置图片frame
    CGRect imageRect = CGRectZero;
    imageRect.origin.y = 3 * kMargin;
    imageRect.size.width = rect.size.width;
    imageRect.size.height = rect.size.height - 3 * kMargin - 20;
    
    // Title label
    if (!self.tabImageView) {
        _tabImageView = [[UIImageView alloc] init];
        [self addSubview:_tabImageView];
    }
    
    _tabImageView.frame = imageRect;
    _tabImageView.contentMode = UIViewContentModeScaleAspectFit;
    if (self.tabImageName) {
        _tabImageView.image = [UIImage imageNamed:self.tabImageName];
        _tabImageView.highlightedImage = [UIImage imageNamed:self.tabHighlightedImageName];
    }
    _tabImageView.highlighted = self.selected;
    
    _tabImageView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    // Title label
    if (!self.tabTitleLabel) {
        UILabel *tabTitleLabel = [[UILabel alloc] init];
        [self addSubview:tabTitleLabel];
        self.tabTitleLabel = tabTitleLabel;
    }
    
    self.tabTitleLabel.text = _tabTitle;
    self.tabTitleLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
    self.tabTitleLabel.textAlignment = NSTextAlignmentCenter;
    
    //设置标题frame
    CGRect labelRect = CGRectZero;
    labelRect.origin.x = 0;
    labelRect.size.width = rect.size.width;
    labelRect.origin.y = rect.size.height - kMargin - 15;
    labelRect.size.height = 15;
    self.tabTitleLabel.frame = labelRect;
    
    self.tabTitleLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    _tabImageView.highlighted = selected;
    _tabTitleLabel.textColor = selected ? [UIColor blueColor] : [UIColor darkGrayColor];
}

@end
